﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleData.ObjectModel;
using SimpleData.Data;
using System.Data.Objects;
using SimpleData.ObjectModel.Design;

namespace DataAccess.SqlClient
{
	
	public class ObjectModelRepository : RepositoryBase, IObjectModelRepository
	{
		public ObjectModelRepository( IConnectionFactory factory )
			: base( factory )
		{ }


		public Schema GetSchema<TFetch>( long schemaID )
			where TFetch : IFetchStrategy<Schema>, new()
		{
			return this.QueryPerTenant<Schema, TFetch>().FirstOrDefault( s => s.SchemaID == schemaID );
		}

		public Schema GetSchema<TFetch>( SchemaName name )
			where TFetch : IFetchStrategy<Schema>, new()
		{
			var data = ( from s in base.QueryPerTenant<Schema, TFetch>()
						 where s.Name.Name == name.Name
							&& s.Name.Namespace == name.Namespace
						 select s );

			return data.FirstOrDefault();
		}
	}
}
